package com.xmx.ndmallwxapi.service;


import com.xmx.common.exception.NMException;
import com.xmx.common.utils.ListFromEntity2VoUtils;
import com.xmx.common.utils.PageUtils;
import com.xmx.common.utils.R;

import com.xmx.ndmallwxapi.feign.CouponFeignClient;
import com.xmx.ndmallwxapi.feign.MallFeignClient;
import com.xmx.ndmallwxapi.feign.ProductFeignClient;
import com.xmx.ndmallwxapi.feign.PromotionFeignClient;
import com.xmx.ndmallwxapi.vo.brand.BranddetailVo;
import com.xmx.ndmallwxapi.vo.goods.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodService {

    @Resource
    ProductFeignClient productFeignClient;

    @Resource
    PromotionFeignClient promotionFeignClient;

    @Resource
    MallFeignClient mallFeignClient;

    @Resource
    CouponFeignClient couponFeignClient;



    public Map detaile(Integer id){
        Map resultMap = new HashMap<String, List>();
        // TODO 1 规格详情
        Map<String,Object> params = new HashMap<>();
        R specificationListR = productFeignClient.listspecification(id);
        List<Map> allspecification = (List<Map>) specificationListR.get("page");
//        存结果
        List<GoodSpecificationVos> goodSpecificationVosList = new ArrayList<>();
        List<GoodSpecificationVo> goodSpecificationVos = ListFromEntity2VoUtils.entity2Vo(allspecification, GoodSpecificationVo.class);
        for (GoodSpecificationVo map : goodSpecificationVos) {
//            判断二层循环是否结束,结束未找到则新增一个，否则追加
            int flag = 0;
//            二层循环判断是否含有规格属性,
            for (int i = 0; i < goodSpecificationVosList.size(); i++) {
                if (goodSpecificationVosList.get(i) != null && goodSpecificationVosList.get(i).getName().equals(map.getSpecification())) {
                    goodSpecificationVosList.get(i).getValueList().add( map);
                    flag = 1;
                }
            }
                if(flag ==0 ){
                    GoodSpecificationVos goodSpecificationVos1 = new GoodSpecificationVos();
                    List<GoodSpecificationVo> ms = new ArrayList<>();
                    ms.add(map);
                    goodSpecificationVos1.setName(map.getSpecification());
                    goodSpecificationVos1.setValueList(ms);
                    goodSpecificationVosList.add(goodSpecificationVos1);
                }
            }
            resultMap.put("specificationList",goodSpecificationVosList);
        params.clear();
        // TODO:  2. 商品团购规则    groupon
        R grouponlistR = promotionFeignClient.getBygoodsid(id);
        List<Map> grouponlist = (List<Map>) ((Map) grouponlistR.get("page")).get("list");
        resultMap.put("groupon", grouponlist);


        // TODO:  3. 商品常见问题    issue
        params.put("limit",5);
        R issuelistR = mallFeignClient.issuelist(params);
        List<Map> issuelist = (List<Map>) ((Map) issuelistR.get("page")).get("list");
        resultMap.put("issue",issuelist);
        params.clear();

        // TODO:  4. user HasCollect	int	用户优惠券数
        // 获取用户id，测试用userid为1
        R getcount = couponFeignClient.getcount(1);
        Integer userHasCollect = (Integer) getcount.get("userHasCollect");
        resultMap.put("userHasCollect",userHasCollect);

        //TODO:  11. 商品详情
        R goodsdetailR =  productFeignClient.info(id);
        List<Map> goodsinfoMap = new  ArrayList<>();
        goodsinfoMap.add((Map)goodsdetailR.get("goods"));
        List<GoodDetailVo> infoVo =  ListFromEntity2VoUtils.entity2Vo(goodsinfoMap, GoodDetailVo.class);
        if(infoVo==null || infoVo.size()==0){
            throw  new NMException("查询商品失败");
        }
        resultMap.put("info",infoVo.get(0));


        // TODO:  5.shareImage	string	商品分享图片
        resultMap.put("shareImage",infoVo.get(0).getShareUrl());

        // TODO:  6. comment	json	商品评论
        R commentR = productFeignClient.listcomment(id);
        List<Map> commentMap = (List<Map>) ((Map)commentR.get("comment")).get("list");
        Integer commentnum = (Integer) ((Map) commentR.get("comment")).get("totalCount");
        Map comments = new HashMap();
        comments.put("data",commentMap);
        comments.put("count",commentnum);
        resultMap.put("comment",comments);

        // TODO:  7. share	boolean	是否商品分享
        resultMap.put("share",infoVo.get(0).getShareUrl()==null?false:true);

        // TODO:  8. attribute	jsonArray	商品参数
        R listgoodattriR = productFeignClient.listgoodattri(id);
        List<Map> listgoodattri =  (List<Map>) ((Map) listgoodattriR.get("page")).get("list");
        List<GoodatrributeVo> goodatrributeVos = ListFromEntity2VoUtils.entity2Vo(listgoodattri,GoodatrributeVo.class);
        resultMap.put("attribute",goodatrributeVos);
        // TODO:  9. brand	json	商品品牌
        R brandR = productFeignClient.getbrand(infoVo.get(0).getBrandId());
        List<Map> brands = new ArrayList<>();
        if(brandR.get("brand")!=null) {
            brands.add((Map) brandR.get("brand"));
            List<BranddetailVo> branddetailVos = ListFromEntity2VoUtils.entity2Vo(brands, BranddetailVo.class);
            resultMap.put("brand", branddetailVos);
        }else
            resultMap.put("brand", null);
        // TODO:  10. productList	jsonArray	商品库存量列表（SKU信息）
        R productListR = productFeignClient.listproduct(id);
        List<Map> productLists =  (List<Map>) ((Map) productListR.get("page")).get("list");
        List<GoodProductVo> goodProductVos = ListFromEntity2VoUtils.entity2Vo(productLists,GoodProductVo.class);
        resultMap.put("productList",goodProductVos);
        return resultMap;
    }

    // TODO:  寻找
    public Map related(Integer id){
        Map resultMap = new HashMap();
        PageUtils related = productFeignClient.related(id);
        List<Map> list = (List<Map>) related.getList();
        List<NewGoodsVo> goodsVos = ListFromEntity2VoUtils.entity2Vo(list,NewGoodsVo.class);
        resultMap.put("total",related.getTotalCount());
        resultMap.put("pages",related.getTotalPage());
        resultMap.put("limit",related.getPageSize());
        resultMap.put("page",related.getCurrPage());
        resultMap.put("list",goodsVos);
        return resultMap;
    }
}
